package argo.review;

public class LongestPalindrome {

    public String longestPalindrome(String s) {

        String maxPal = "";

        for (int i = 0; i < s.length(); i++) {
            String s1 = longestPalindrome(s, i, i);
            String s2 = longestPalindrome(s, i, i + 1);
            String ss = s1.length() > s2.length() ? s1 : s2;
            maxPal = maxPal.length() > ss.length() ? maxPal : ss;
        }

        return maxPal;

    }

    public String longestPalindrome(String s, int left, int right) {
        while (left > 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            left--;
            right++;
        }

        if (left < 0 || right >= s.length() || s.charAt(left) != s.charAt(right)) {
            left++;
            right--;
        }

        return s.substring(left, right + 1);

    }

}
